package x;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.net.URL;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.imageio.ImageIO;

import javax.sound.sampled.Clip;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.AudioInputStream;
import javax.imageio.ImageIO;


/**
 * The map holds the data about game area. In this case its responsible
 * for both rendering the map and check collision against the grid cells
 * within.
 *
 * Our map is a simple WIDTHxHEIGHT grid containing value 0 to indicate
 * a clear cell and 1 to indicate a wall. private Image mback1;
 *
 * @author Daniel Wheeler
 */
public class Map {
    //the ground for the map
        private Image floor;
                /**the bar**/
       
        /** The map which this entity is wandering around */
        /** The value indicating a clear cell */
        private static final int CLEAR = 0;
        /** The value indicating a blocked cell */
        private static final int BLOCKED = 1;

        private static final int OUTERBLOCKED = 8;

        public static final int buildingcomplete= 7;
        //the above line is clearly meant for an 8 hour designed building
        private Image mback1;
        public int location;
        //cell for building door
        private static final int DOOR= 3;
        private static final int DOOR2= 4;
        private static final int DOOR3= 5;
        private static final int DOOR4= 6;

        
        /** The width in grid cells of our map */
        private static final int WIDTH = 64;
        /** The height in grid cells of our map */
        private static final int HEIGHT = 48;

        /** The rendered size of the tile (in pixels) */
        public static final int TILE_SIZE = 10;

        /** The actual data for our map */
        private int[][] data = new int[WIDTH][HEIGHT];
        private Entity MapMain;
        /**
         * Create a new map with some default contents
         */
        public Map() {

          //using try and catch to grab the main background image, that will be the supporting background wallpaper for this map

                try{
                    URL mapbackground1= Thread.currentThread().getContextClassLoader().getResource("x/MainMap.jpg");

                    mback1 = ImageIO.read(mapbackground1);

                    MapMain=new Entity(mback1,this,1,1);



                }
                catch (IOException e) {
                        System.err.println("Unable to find Main Background Image, it will now terminate");
                        System.exit(0);
                }




/*----------------------------------------------Collision---level design----------------------*/

            //this makes vertical blocks and horizontal.......may change!
                for (int y=0;y<HEIGHT;y++)
                {
                        data[0][y] = BLOCKED;
                        data[WIDTH - 1][y] = BLOCKED;
                       for (int x=0;x<WIDTH;x++)
                   {

                     data[x][0] = BLOCKED;

                     data[x][HEIGHT - 1] = BLOCKED;

                     }
                }





                // This is building One, not sure the name of the building yet
                for(int side1=5; side1<=20; side1++)
                {
                     data[7][side1]= buildingcomplete;
                }

                    for(int side2=5; side2<=20; side2++)
                    {
                        data[24][side2]= buildingcomplete;
                    }

                        for(int side3=8; side3<=24 ;side3++)
                        {
                            data[side3][5]=buildingcomplete;
                        }
                            for(int side4=8; side4<=24 ;side4++)
                            {
                                data[side4][20]=buildingcomplete;
                            }
                            //end building 1





                // This is building two, again not sure of the building yet
                for(int side5=5;side5<=20;side5++)
                {
                    data[WIDTH-8][side5]=buildingcomplete;
                }
                    for(int side6=5;side6<=20;side6++)
                    {
                        data[WIDTH-25][side6]=buildingcomplete;
                    }

                        for(int side7=39;side7<=55;side7++)
                        {
                            data[side7][20]=buildingcomplete;
                        }
                            for(int side8=39;side8<=55;side8++)
                            {
                                data[side8][5]=buildingcomplete;
                            }
                            //end building  2

                //----------bottom buildings

                // I got a little crazy here i need to clean this mess up!
                for(int side9=28;side9<=42;side9++)
                {
                    data[7][side9]=BLOCKED;
                }
                    for(int side10=28;side10<=42;side10++)
                    {
                        data[24][side10]=BLOCKED;
                    }
                        for(int side11=28;side11<=42;side11++)
                        {
                            data[39][side11]=BLOCKED;
                        }
                            for(int side12=28;side12<=42;side12++)
                            {
                                  data[56][side12]=BLOCKED;
                            }
                                for(int side13=7;side13<=24;side13++)
                                {
                                    data[side13][27]=BLOCKED;
                                }
                                    for(int side14=7;side14<=24;side14++)
                                    {
                                        data[side14][42]=BLOCKED;
                                    }
                                       for(int side15=39;side15<=55;side15++)
                                       {
                                           data[side15][42]=BLOCKED;
                                       }
                                        for(int side16=39;side16<=56;side16++)
                                        {
                                            data[side16][27]=BLOCKED;
                                        }


                //door 1
               data[15][20] = DOOR;
               data[14][20] = DOOR;
               data[16][20] = DOOR;
               data[17][20] = DOOR;
               

               //door3
               data[WIDTH -15][HEIGHT -6]= DOOR3;
               data[WIDTH -16][HEIGHT -6]= DOOR3;
               data[WIDTH -17][HEIGHT -6]= DOOR3;
               data[WIDTH -18][HEIGHT -6]= DOOR3;

               //door 2
               data[WIDTH -15][20]= DOOR2;
               data[WIDTH -16][20]= DOOR2;
               data[WIDTH -17][20]= DOOR2;
               data[WIDTH -18][20]= DOOR2;
              

               //door4
               data[15][HEIGHT -6] = DOOR;
               data[14][HEIGHT -6] = DOOR;
               data[16][HEIGHT -6] = DOOR;
               data[17][HEIGHT -6] = DOOR;




        }

        /*---------------------------------------------------------------------------------*/
        /**
         * Render the map to the graphics context provided. The rendering
         * is just simple fill rectangles
         *ull
         */


        public void paint(Graphics2D g) {
                // loop through all the tiles in the map rendering them
                // based on whether they block or not
                for (int x=0;x<WIDTH;x++) {
                        for (int y=0;y<HEIGHT;y++) {

                                


                                   
                                        g.setColor(Color.green);
                                   if (data[x][y] == BLOCKED)
                                        g.setColor(Color.green);

                                   if(data[x][y] == buildingcomplete)
                                       g.setColor(Color.green);
                                                                   
                                   if(data[x][y] == DOOR)
                                       g.setColor(Color.green);

                                   if(data[x][y]== DOOR2)
                                       g.setColor(Color.green);

                                   if(data[x][y]== DOOR3)
                                       g.setColor(Color.green);

                                   if(data[x][y]== DOOR4)
                                       g.setColor(Color.green);
                                // draw the rectangle with a dark outline
                                g.fillRect(x*TILE_SIZE,y*TILE_SIZE,TILE_SIZE,TILE_SIZE);
                                //g.setColor(g.getColor().darker());
                                g.drawRect(x*TILE_SIZE,y*TILE_SIZE,TILE_SIZE,TILE_SIZE);
                        }
                }





        }

        /**
         * Check if a particular location on the map is blocked. Note
         * that the x and y parameters are floating point numbers meaning
         * that we can be checking partially across a grid cell.
         *
         * @param x The x position to check for blocking
         * @param y The y position to check for blocking
         * @return True if the location is blocked
         */
        public boolean blocked(float x, float y)
        {
                // look up the right cell (based on simply rounding the floating
                // values) and check the value
                if (data[(int) x][(int) y] == BLOCKED)
                return data[(int) x][(int) y] == BLOCKED;

                

                else return false; //this should NEVER have to be called

        }

        public boolean buildingcomplete(float x, float y)
        {
             if (data[(int) x][(int) y] == buildingcomplete)
                return data[(int) x][(int) y] == buildingcomplete;

                
                else return false; //this should NEVER have to be called


        }

       /* public boolean OUTERBLOCKED(float x, float y)
        {
             if (data[(int) x][(int) y] == buildingcomplete)
                return data[(int) x][(int) y] == buildingcomplete;


                else return false; //this should NEVER have to be called


        }*/
        //function place the buildings that you can go inside of buildings
        //returns the # of the building
        public int buildingChecker(float x, float y){

//convert the float numbers to ints
int x2 = (int) x;
int y2 = (int) y;

        location = -1;

            if ((x2 == 15 || x2 == 14 || x2 == 16 || x2 == 17 || x2 == 18 || x2 == 19)&& y2 == 20)
             location = 1; //location is upper left building, minigame 1

            if ((x2 == WIDTH -14 || x2 == WIDTH -13 || x2 == WIDTH-15 || x2 == WIDTH-14 || x2 == WIDTH-16 || x2 == WIDTH-17)&& y2 == 20)
             location = 2; //location is upper left building, minigame 2

            if ((x2 == 15 || x2 == 14 || x2 == 16 || x2 == 17)&& y2 == HEIGHT -6)
             location = 3; //location is upper left building, minigame 3

            if ((x2 == WIDTH-15 || x2 == WIDTH-14 || x2 == WIDTH-16 || x2 == WIDTH-17)&& y2 == HEIGHT -6)
             location = 4; //location is upper left building, minigame 4



        //if (data[(int) x][(int) y] == BLOCKED)

        //if (data[(int) x][(int) y] == BLOCKED)
            return location;

 }


}
